package com.basic.library.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.basic.library.R;
import com.basic.library.utils.FlymeStatusbarColorUtils;
import com.basic.library.utils.ScreenUtils;
import com.basic.library.utils.SystemBarTintManager;

/**
 * @author yj
 * @description
 */
public class FullScreenDialog extends Dialog {
    public FullScreenDialog(@NonNull Context context) {
        super(context, R.style.dialog_Full);
    }


    View root = null;
    int resId, mVariableId;
    Object data;
    protected boolean statusBarTextColorBlack = false; //状态栏字体颜色 变化标志  true黑色。反之白色

    public FullScreenDialog setData(int resId, int mVariableId, Object data) {
        this.resId = resId;
        this.mVariableId = mVariableId;
        this.data = data;
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
//        <!--关键点1-- >
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        setStatusBarTrans();
        super.onCreate(savedInstanceState);
        ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(this.getContext()), resId, null, false);
        binding.setVariable(mVariableId, data);
        root = binding.getRoot();
//        <!--关键点2-- >
        setContentView(root);
//        <!--关键点3-- >
        getWindow().setBackgroundDrawable(new ColorDrawable(0x00000000));
//        <!--关键点4-- >
        getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
    }

    public FullScreenDialog dismissOrShow() {
        if (isShowing())
            dismiss();
        else
            show();
        return this;
    }


    protected boolean needFullScreen = false;

    private void setStatusBarTrans() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < 23) {//6.0以下系统
            if (!ScreenUtils.MIUISetStatusBarLightMode(getWindow(), true)) {//小米的用自己的方法，不成功的话，再加个遮罩
                // 创建状态栏的管理实例
                SystemBarTintManager tintManager = new SystemBarTintManager((Activity) getContext());
                // 激活状态栏设置
                tintManager.setStatusBarTintEnabled(true);
                // 设置一个颜色给系统栏
                tintManager.setTintColor(getContext().getResources().getColor(com.basic.library.R.color.statusbar_color));
            }
        } else if (Build.VERSION.SDK_INT >= 23) { //下面方法设置了以后，style里的沉浸式其实已经没有用了。
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            if (statusBarTextColorBlack) {
                if (needFullScreen) {
                    window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                } else {
                    window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                }
                FlymeStatusbarColorUtils.setStatusBarDarkIcon(getWindow(), true);
                ScreenUtils.MIUISetStatusBarLightMode(getWindow(), statusBarTextColorBlack);
            } else {
                if (needFullScreen) {
                    window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                } else {
                    window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                }
            }
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        }
    }
}
